package com.hgy.fxui.controller;

import com.hgy.fxui.HospitalDrugApplication;
import com.hgy.fxui.model.User;
import com.hgy.fxui.service.UserService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class LoginController {
    @FXML
    private TextField usernameField;
    
    @FXML
    private PasswordField passwordField;

    @Autowired
    private UserService userService;

    // 窗体尺寸常量
    private static final double MAIN_WINDOW_WIDTH = 1500;
    private static final double MAIN_WINDOW_HEIGHT = 900;
    private static final double MIN_WINDOW_WIDTH = 1000;
    private static final double MIN_WINDOW_HEIGHT = 700;
    
    @FXML
    private void handleLogin() {
        String username = usernameField.getText();
        String password = passwordField.getText();
        
        if (username == null || username.trim().isEmpty()) {
            showError("请输入用户名");
            return;
        }
        
        if (password == null || password.trim().isEmpty()) {
            showError("请输入密码");
            return;
        }

        User user = userService.authenticateUser(username, password);
        
        if (user != null) {
            try {
                ConfigurableApplicationContext springContext = HospitalDrugApplication.getSpringContext();

                // 1. 加载主界面
                FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"));
                mainLoader.setControllerFactory(springContext::getBean);
                Scene mainScene = new Scene(mainLoader.load());
                
                // 2. 获取主界面控制器
                MainController mainController = mainLoader.getController();

                // 3. 获取并设置Stage
                Stage currentStage = getCurrentStage(mainScene);
                
                // 4. 加载数据大屏
                mainController.showDashboard();
                
                // 5. 显示主界面
                currentStage.show();
            } catch (Exception e) {
                log.error("加载主界面失败", e);
                showError("加载主界面失败！");
            }
        } else {
            showError("用户名或密码错误！");
        }
    }

    private void showError(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("错误");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    /**
     * 获取当前Stage并设置尺寸
     * @param mainScene
     * @return
     */
    private Stage getCurrentStage(Scene mainScene) {
        Stage currentStage = (Stage) usernameField.getScene().getWindow();

        // 1. 清除旧的尺寸限制
        currentStage.setMinWidth(0);
        currentStage.setMinHeight(0);

        // 2. 显式设置新尺寸
        currentStage.setWidth(MAIN_WINDOW_WIDTH);
        currentStage.setHeight(MAIN_WINDOW_HEIGHT);

        // 3. 设置新最小限制
        currentStage.setMinWidth(MIN_WINDOW_WIDTH);
        currentStage.setMinHeight(MIN_WINDOW_HEIGHT);

        currentStage.setTitle("医药管理系统");
        currentStage.setScene(mainScene);
        currentStage.centerOnScreen();
        return currentStage;
    }

    @FXML
    private void handleReset() {
        usernameField.clear();
        passwordField.clear();
    }
}